package invest.data.getdata;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import invest.data.Data;
import invest.pojo.datapojo.Stock;
import invest.utils.CodeConverterUtil;
import invest.utils.HttpRequest;

import java.util.ArrayList;
import java.util.List;

public class HKStockRealtimeData implements Data<List<Stock>> {
    final String url="https://xueqiu.com/service/v5/stock/screener/quote/list?page=1&size=3000&order=desc&orderby=amount&order_by=percent&market=HK&type=hk&is_delay=true";
    @Override
    public List<Stock> getData() {
        String result = HttpRequest.sendGet(url, null);
        JSONObject jb = JSONObject.parseObject(result);
        JSONObject data=jb.getJSONObject("data");
        JSONArray jsonArray = data.getJSONArray("list");
        List<Stock> stocksList=new ArrayList<Stock>();
        for (int i = 0; i <jsonArray.size() ; i++) {
            JSONObject jsonObject=jsonArray.getJSONObject(i);
            Stock stock = JSON.toJavaObject(jsonObject, Stock.class);
            stock.setSymbol(CodeConverterUtil.convertCode(stock.getSymbol()));
            stocksList.add(stock);
        }
        return stocksList;
    }

    public static void main(String[] args) {
        HKStockRealtimeData hkStockRealtimeData = new HKStockRealtimeData();
        List<Stock> stocks = hkStockRealtimeData.getData();
        System.out.println(1);
    }
}
